package com.thinkgem.jeesite.cn.yy.action;

import com.thinkgem.jeesite.cn.yy.service.ProTableService;
import com.thinkgem.jeesite.cn.yy.utils.Paging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * Created by jws24 on 2017/11/22.
 */
@Controller
@RequestMapping("ProTable")
public class ProTableAction {

    @Autowired
    ProTableService proTableService;

    private final Logger logger = LoggerFactory.getLogger(ProTableAction.class);

    /**
     * 增加数据表
     * @param tabName
     * @param proId
     * @param tabDescription
     * @return
     */
    @RequestMapping("saveProTable")
    @ResponseBody
    public Map<String, String> saveProTable(
            @RequestParam(value = "tab_name", defaultValue = "") String tabName,

            @RequestParam(value = "pro_id", defaultValue = "") String proId,
            @RequestParam(value = "tab_description",defaultValue = "")String tabDescription
    ) {
              int id=Integer.parseInt(proId);
             Map<String, String> map = new HashMap<String, String>();
             int  list=proTableService.selectId(id,tabName);
             logger.info("{}",list);
               System.out.println(tabName);
                if (list==0) {
                    proTableService.saveProTable(tabName, id, tabDescription);
                    proTableService.saveSaveTableAction(id);
                    map.put("message", "OK");
                    return map;
                } else {

                    map.put("message", "ERROR");
                    return map;
                }


    }

    /**
     * 根据项目名查表
     *
     * @return
     */
    @RequestMapping("getProTableByName")
    @ResponseBody
    public Map<String, Object> getProTable(
            HttpServletRequest request, @RequestParam(value = "pro_name", defaultValue = "") String proName
    ) {
        int count = proTableService.getProTablePage();
        Map<String, Object> map = new HashMap();
        if (count == 0) {
            map.put("message", "ERROR");
            return map;
        } else {
            int first = Integer.parseInt(request.getSession().getServletContext().getAttribute("pageSize").toString());
            Paging paging = new Paging(0, first, count);
            List<Map<String, String>> proTableList = proTableService.getProTableByName(proName, paging);
            System.out.println("______________________________________________________________");
            logger.info("{}", proTableList);
            //String actName = "全查出"+ tabName +"表";
            if (proTableList != null) {
                proTableService.saveTableActionByProName(proName);
                if (proName != null) {
                    map.put("message", "OK");

                    map.put("data",proTableList);
                    return map;
                } else {
                    map.put("message", "ERROR");
                    return map;
                }
            } else {
                map.put("message", "ERROR");
                return map;
            }
        }

    }



    /**
     * 更改数据库信息表
     *
     * @param tabName
     * @return
     */
    @RequestMapping("updateProTable")
    @ResponseBody
    public Map<String, String> updateProTable(
            @RequestParam(value = "tab_name", defaultValue = "") String tabName,
            @RequestParam(value = "tab_description",defaultValue = "")String tabDescription,
            @RequestParam(value = "tab_id", defaultValue = "")int tabId) {
        Map<String, String> map = new HashMap<String, String>();
        int  list=proTableService.byId(tabId,tabName);
            if(list==0 ){
                proTableService.saveUpdateTableAction(tabId,tabName);
                proTableService.updateProTable(tabId, tabName,tabDescription);
                map.put("message", "OK");
                return map;
            }else{
                map.put("message", "ERROR");
                return map;
            }


        }



    /**
     * 删除表中信息
     */
    @RequestMapping("delProTable")
    @ResponseBody

    public Map<String, String> delProTable(@RequestParam(value = "tab_id", defaultValue = "") int tabId
    ) {
        Map<String, String> map = new HashMap<String, String>();
        String a=Integer.toString(tabId);
        if (a != null ) {
            proTableService.saveDelTableAction(tabId);
            proTableService.delProTable(tabId);
            map.put("message", "OK");
            return map;
        } else {
            map.put("message", "ERROR");
            return map;
        }


    }

}